<?php
include_once dirname(__FILE__)."/../../model/config.php";
		$_etat=new etat();
if(isset($_POST["operation"])){
if($_POST["operation"]=="INSERT"){
		if(isset($_POST["code_etat"])){$_etat->set_code_etat($_POST["code_etat"]);}
		if(isset($_POST["decision"])){$_etat->set_decision($_POST["decision"]);}
		if(isset($_POST["code_ue"])){$_etat->set_code_ue($_POST["code_ue"]);}
		if(isset($_POST["date_decision"])){$_etat->set_date_decision($_POST["date_decision"]);}
		$res=$_etat->INSERT(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}
elseif($_POST["operation"]=="DELETE"){
		if(isset($_POST["code_etat"])){
		$_etat->set_code_etat($_POST["code_etat"]);
		$res=$_etat->DELETE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
		}
}
elseif($_POST["operation"]=="UPDATE"){
		if(isset($_POST["code_etat"])){$_etat->set_code_etat($_POST["code_etat"]);}
		if(isset($_POST["decision"])){$_etat->set_decision($_POST["decision"]);}
		if(isset($_POST["code_ue"])){$_etat->set_code_ue($_POST["code_ue"]);}
		if(isset($_POST["date_decision"])){$_etat->set_date_decision($_POST["date_decision"]);}
		$res=$_etat->UPDATE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}

}
else{
	$constraint='';$limit=0;$start=0;$pagination='';
	if(isset($_POST["code_etat"]) && !empty($_POST["code_etat"])){
		$constraint.=" AND code_etat='".$_POST["code_etat"]."'";
	}

	if(isset($_POST["decision"]) && !empty($_POST["decision"])){
		$constraint.=" AND decision='".$_POST["decision"]."'";
	}

	if(isset($_POST["code_ue"]) && !empty($_POST["code_ue"])){
		$constraint.=" AND code_ue='".$_POST["code_ue"]."'";
	}

	if(isset($_POST["date_decision"]) && !empty($_POST["date_decision"])){
		$constraint.=" AND date_decision='".$_POST["date_decision"]."'";
	}

	if(isset($_POST['limit'])){$limit=$_POST['limit'];}
	if(isset($_POST['start'])){$start=$_POST['start'];}
	if($limit>0){
		$pagination.=" LIMIT  $start,$limit"; 
}
		$res=$_etat->SELECT($constraint,$pagination); 

		 $data=array();
		  while($row=$_etat->fetch_object()){
		  	$data[]=$row;
	  	  }
  	 	 
		
	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('totalcount' => count($data),'result' => $data ));
	  	
	  }}
?>